/*
 * @Author: aggfb 714740235@qq.com
 * @Date: 2024-05-19 08:16:41
 * @LastEditors: aggfb 714740235@qq.com
 * @LastEditTime: 2024-05-25 22:45:46
 * @FilePath: \label_rack_control\firmware\system\apm_103\main.c
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */

#include "main.h"
#include <stdio.h>
#include "ulog.h"


/* 
    1ms 系统时钟 
*/
static uint64_t system_times = 0; 
uint64_t main_get_system_times(void)
{
    return system_times;
}

/**
 * 1ms 定时器中断
*/
void TMR4_IRQHandler(void)
{
    if(TMR_ReadIntFlag(TMR4, TMR_INT_UPDATE) == SET)
    {
        TMR_ClearIntFlag(TMR4, TMR_INT_UPDATE);
        system_times++;
        
        static uint16_t count_500ms = 0;
        if(++count_500ms >= 500)
        {
            count_500ms = 0;
            bsp_gpio_led_toggle();
        }
    }
}

int main(void)
{
    /* 
        1.bsp外设初始化
        2.ui界面初始化 ringbuffer
     */
    bsp_system_init(); 
    ui_screen_init();
    bsp_delay_ms(500);
    
    /* 读取保存参数,并且同步到界面 */
    motor_ctrl_t* motor_ctrl = motor_ctrl_dev();  
    param_gain(); 
    bsp_delay_ms(500); 
    ui_screen_sned_offset(motor_ctrl->offset_cmd); /* 偏移量 */
    bsp_delay_ms(200); 
    ui_screen_sned_speed(motor_ctrl->speed_ratio_cmd);  /* 速度 */
    bsp_delay_ms(200);

    /* 打印系统信息 */
    #if (LOG_OUTPUT_EN == 1)
    SEGGER_RTT_printf(0, "system offset = [%d];speed = [%d].\r\n", \
                         motor_ctrl->offset_cmd, \
                         motor_ctrl->speed_ratio_cmd);
    #endif

#if 1
    IWDT_EnableWriteAccess();
    IWDT_ConfigDivider(IWDT_DIVIDER_32);
    IWDT_ConfigReload(300);
    IWDT_Enable();
#endif
    while(1)
    {
        IWDT_Refresh();
        {
            app_main(); /* 主逻辑 */
        }
        {
            ui_screen_recv_handle(); /* 交互解析 */
        }       
    }
}
